package com.thesis.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Student;
import com.thesis.manage.domain.Thesis;
import com.thesis.manage.service.ThesisService;
import com.thesis.manage.service.model.ExamineEnum;
import com.thesis.manage.service.model.RoleEnum;
import com.thesis.manage.service.model.ThesisTypeEnum;
import com.thesis.manage.service.model.TimeTypeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/3/14
 */
@RequestMapping("/thesis")
@RestController
public class ThesisController extends BaseController {

    @Resource
    private ThesisService thesisService;

    /**
     * 查看申请论题状态
     */
    @GetMapping("/application/check")
    public Map<String, Object> checkOpenApplication() {
        return buildMessage(() -> thesisService.checkOpenApplication(TimeTypeEnum.APPLY.getValue()));
    }

    /**
     * 返回论题类型
     */
    @GetMapping("/types")
    public Map<String, Object> getThesisType() {
        return buildMessage(ThesisTypeEnum::getList);
    }

    /**
     * 申请论题
     *
     * @param thesis 论题
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody Thesis thesis) {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            thesis.setPersonType(RoleEnum.STUDENT.getValue());
            thesis.setPerson(student.getName());
            thesis.setPersonId(student.getId());
            thesis.setCollegeId(student.getCollegeId());
            thesis.setPub(YesOrNoEnum.NO.getValue());
            thesis.setStatus(ExamineEnum.TEACHER_EXAMINEING.getValue());
            thesisService.save(thesis);
            return null;
        });
    }

    /**
     * 查询自己的论题
     */
    @GetMapping("/person/list")
    public Map<String, Object> findAllByPerson() {
        return buildMessage(() -> {
            Result result = getUser();
            Student student = JSONObject.parseObject(result.getData().toString(), Student.class);
            Thesis thesis = new Thesis();
            thesis.setPersonId(student.getId());
            return thesisService.findAllByPerson(thesis);
        });
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/{id}/delete")
    public Map<String, Object> delete(@PathVariable Long id) {
        return buildMessage(() -> {
            thesisService.deleteById(id);
            return null;
        });
    }

}
